<template>
  <div id="login2">
    <div class="container2">
      <div class="title2">忘记密码</div>
      <el-form ref="form" :model="form" :rules="rules" class="form-container">
        <el-form-item prop="uname">
          <el-input placeholder="请输入用户名" prefix-icon="el-icon-user" size="medium" v-model="form.uname"></el-input>
        </el-form-item>
        <el-form-item prop="email">
          <el-input placeholder="请输入QQ邮箱" prefix-icon="el-icon-message" size="medium" v-model="form.email"></el-input>
        </el-form-item>



        <el-form-item>
          <div class="button-row2">
            <el-button size="medium" type="primary" :disabled="isSending" @click="sendVerificationCode" :style="buttonStyle2">{{ buttonLabel }}</el-button>
          </div>
          <div class="button-row2">
            <el-button size="medium" type="primary" @click="retur" :style="buttonStyle2">
              返回登录页面
            </el-button>
          </div>
        </el-form-item>

      </el-form>
    </div>
  </div>
</template>

<script>
import request from "@/utils/request";

export default {
  name: "LoginForget",
  data() {
    return {
      form: {},
      rules: {
        uname:[{required: true, message: '请输入账号', trigger: 'blur'},
          // {min: 1, max: 6, message: '请输入正确的账号!', trigger: 'blur'}
        ],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' },
          {
            pattern: /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/,
            message: '请输入有效的邮箱地址',
            trigger: ['blur', 'change']
          }]
      },
      isSending: false, // 是否正在发送验证码
      countDown: 60, // 倒计时时间（秒）
    };
  },
  computed: {
    buttonStyle2() {
      const inputWidth = 300; // 输入框的宽度
      const buttonPadding = 10; // 按钮的内边距
      const buttonIconWidth = 14; // 按钮图标的宽度
      return `width: calc(${inputWidth}px - ${buttonPadding * 2}px - ${buttonIconWidth}px)`;
    },
    buttonLabel() {
      if (this.isSending) {
        return `${this.countDown}秒后可重发`;
      } else {
        return '找回密码';
      }
    },
  },
  methods: {
    sendVerificationCode() {
      // 检查表单是否有效
      this.$refs.form.validateField('email', (errorMsg) => {
        if (errorMsg) {
          return;
        }

        //发送验证码
        request.post('send/emailpwd', this.form).then((res) => {
          if (res.code === '200') {
            // 发送成功后启用倒计时
            this.startCountdown();
            this.$notify.success("密码已发送至您的邮箱，请注意查收！");
            // setTimeout(() => {
            //   this.$router.push('/login');
            // }, 3000);

          } else {
            this.$notify.error(res.msg);
          }
        });
      });
    },
    retur(){
      this.$router.push('/login');
    },
    startCountdown() {
      this.isSending = true; // 启用倒计时按钮
      const timer = setInterval(() => {
        if (this.countDown > 0) {
          this.countDown--;
        } else {
          clearInterval(timer);
          this.isSending = false; // 结束倒计时，启用发送按钮
          this.countDown = 60; // 重设倒计时时间
        }
      }, 1000);
    },
    register() {
      // 提交表单逻辑
      this.$refs.form.validate((valid) => {
        if (valid) {
          // 发起注册请求
          request.post('admin/forget', this.form).then((res) => {
            console.log(res);
            if (res.code === '200') {
              // this.$notify.success("注册成功！");
              this.form={}
            } else {
              this.$notify.error(res.msg);
            }
          });
        }
      });
    },
  },
};
</script>


<style scoped>
#login2 {
  height: 100vh;
  overflow: hidden;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #f5f7fa;
}
.container2 {
  width: 400px;
  margin: 0 auto;
  padding: 20px;
  background-color: #f5f7fa;
  border-radius: 4px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.title2 {
  font-size: 24px;
  text-align: center;
  color: dodgerblue;
  margin-bottom: 20px;
}

.verification-code-wrapper {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  margin-bottom: 10px;
}

.button-row2 {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}

</style>
